﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;

using cc2530.Lib;

using System.Windows.Forms;
using System.Drawing.Text;
using System.Drawing;


namespace cc2530.Lib.contiki
{
    public enum contikiRequest{
         REQUEST_TYPE_ND        = 1,
         REQUEST_TYPE_RT        = 2,
         REQUEST_TYPE_DRT       = 3,
         REQUEST_TYPE_ADDR      = 4,
         REQUEST_TYPE_TOTALS    = 0xFF,
    }


    public enum contikiNodeState{
          NBR_INCOMPLETE =0,
          NBR_REACHABLE =1,
          NBR_STALE =2,
          NBR_DELAY =3,
          NBR_PROBE =4,
    }



   public enum NodeType {   
        BorderRouter = 1,
        Router = 2,
        Node = 3,
        PCnode = 4,
    }
    /// <summary>
    /// 一个节点
    /// </summary>
   public  class contikiNode
    {
       //节点名字，以后64位为名字
       public String name { get { return IPv6Address.AddressToString(this.IP).Substring(20); }}
        //mac 地址：mac 64--->这个不是 标准的 48 位地址
        public  byte[] Mac = new byte[8];
        //ip 地址:本地ip
        public IPAddress IP;

       /// <summary>
       /// 状态
       /// </summary>
       public contikiNodeState state;


       /// <summary>
       /// 邻居节点
       /// </summary>
       public List<contikiNode> Neighbors = new List<contikiNode>();
       /// <summary>
       /// 节点所属链表
       /// </summary>
       public List<contikiNode> BelongTo; 


       /// <summary>
       /// 节点的位置
       /// </summary>
       public Point location = GetARandomLocation();
       static int Xmax = 200; //以m为单位 
       static int Ymax = 200; //以m为单位
       public Point locationMax {get {return new Point(Xmax, Ymax);}}
       static public Point GetARandomLocation(){

           long i = System.DateTime.Now.Ticks;
           int x = new Random((int)i).Next(Xmax);
           i = x * i;
           int y = new Random((int)i).Next(Ymax);
           x *= x; x %= Xmax;
           y *= y; y %= Ymax;
           return new Point(x,y);

       }


       /// <summary>
       /// 节点类型
       /// </summary>
       public NodeType ntype = NodeType.Router;

        #region 构造

        public contikiNode():base(){
        }

        public contikiNode(byte[] rimeaddress):base(){
            if (rimeaddress != null && rimeaddress.Length > 0) {
                Array.Copy(rimeaddress, this.Mac, 8);
            }
        }

        public contikiNode(IPAddress ip) : base() { IP = ip;}

        #endregion

        public contikiTreeNode ToTreeNode()
        {
            contikiTreeNode root = new contikiTreeNode();
            foreach (contikiNode node in this.BelongTo) { 
                contikiTreeNode trnode = new contikiTreeNode();
                trnode.contikinode =  node;
                trnode.Name = node.name;
                root.Nodes.Add(trnode);
            }
            return root;
        }


    }

    public class contikiTreeNode:TreeNode
    {
        public contikiNode contikinode;
        public contikiTreeNode():base(){}

    }


}
